package com.bdcom.dialog;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.text.NumberFormatter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bdcom.comp.CanvasPanel;
import com.bdcom.util.PlannerUtil;

@Component
public class SignRangeCfgDialog extends JDialog implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JFormattedTextField minSignField = null;

	private JFormattedTextField maxSignField = null;

	private JLabel minLabel = null;

	private JLabel maxLabel = null;

	private JButton okBtn = null;
	@Autowired
	private CanvasPanel canvasPanel;
	@Autowired
	private JSlider signSlider;

	// private final String CMD1 = "set Min and Max Sign";

	public SignRangeCfgDialog() {

		JPanel panel = new JPanel();

		GridBagLayout baglayout = new GridBagLayout();

		GridBagConstraints constraints = new GridBagConstraints();

		panel.setLayout(baglayout);

		NumberFormatter formater = new NumberFormatter();

		formater.setValueClass(Integer.class);

		minSignField = new JFormattedTextField(formater);
		
		minSignField.setPreferredSize(new Dimension(100, 20));
		
		maxSignField = new JFormattedTextField(formater);
		
		maxSignField.setPreferredSize(new Dimension(100, 20));
		
		minLabel = new JLabel(PlannerUtil.getString("minimum"));


		maxLabel = new JLabel(PlannerUtil.getString("maximum"));

		okBtn = new JButton(PlannerUtil.getString("save"));

		okBtn.addActionListener(this);

		constraints.insets=new Insets(5, 0, 0, 10);
		
		constraints.gridx = 0;

		panel.add(minLabel, constraints);

		constraints.gridx = 1;

		panel.add(minSignField, constraints);

		constraints.gridx = 0;

		panel.add(maxLabel, constraints);

		constraints.gridx = 1;

		panel.add(maxSignField, constraints);

		constraints.gridwidth = 2;

		constraints.gridx = 0;
		
		constraints.insets=new Insets(20, 0, 0, 10);

		panel.add(okBtn, constraints);

		this.getContentPane().add(panel);

		this.setTitle(PlannerUtil.getString("set sign range"));

		this.setSize(300, 200);

		this.setDefaultCloseOperation(HIDE_ON_CLOSE);

		this.setModal(true);

		this.setLocationRelativeTo(null);

		this.setResizable(false);
	}

	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);

		minSignField.setValue(canvasPanel.getCanvasModel().getMinSign());

		maxSignField.setValue(canvasPanel.getCanvasModel().getMaxSign());
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		int min = -85;

		int max = -10;
		try {
			min = (int) minSignField.getValue();

			max = (int) maxSignField.getValue();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(this, PlannerUtil.getString("Please input valid value"));
			return;
		}

		if (max > -10) {

			JOptionPane.showMessageDialog(null, PlannerUtil.getString("Invalid maximum: can not > -10"));

			return;

		}

		if (min < -85) {

			JOptionPane.showMessageDialog(null, PlannerUtil.getString("Invalid minimum: can not < -85"));

			return;

		}

		if (min + 10 > max) {

			JOptionPane.showMessageDialog(null, PlannerUtil.getString("Invalid range: can not < 10dbm"));

			return;
		}

		canvasPanel.getCanvasModel().setMinSign(min);

		canvasPanel.getCanvasModel().setMaxSign(max);

		if (canvasPanel.getCanvasModel().getMinSignToShow() < min) {
			canvasPanel.getCanvasModel().setMinSignToShow(min);
		}
		this.setVisible(false);

		signSlider.repaint();
	}

}
